package com.ctg.behavior.calc.impala.transfer.addictions;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.report.addictions.CtAddictionsTimesReport;
import com.ctg.behavior.calc.impala.transfer.CtFilterTransfer;
import com.ctg.behavior.common.addictions.CtAddictionsCalcBody;
import com.ctg.behavior.common.calc.CtCalc;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtAddictionsTransfer extends CtFilterTransfer {

	@Autowired
	private CtAddictionsTimesTransfer timesTransfer;

	@Override
	public String doTransfer(CtCalc calcObject, CtSQLParam sqlParam) {

		if (!(calcObject instanceof CtAddictionsCalcBody)) {
			log.warn("calcObject is not CtAddictionsCalcBody, class={}", calcObject.getClass());
			log.warn("calcObject: {}", JSON.toJSON(calcObject));
			return "";
		}
		CtAddictionsCalcBody report = (CtAddictionsCalcBody) calcObject;
		log.info("report={}", JSON.toJSONString(report));

		StringBuilder impalaSQL = new StringBuilder();

		List<CtAddictionsTimesReport> timesReports = new ArrayList<>();
		for (int timesReportIndex = 0; timesReportIndex < timesReports.size(); timesReportIndex++) {
			String timesReportSQL = timesTransfer.doTransfer(timesReports.get(timesReportIndex), sqlParam);
			if (timesReportIndex > 0) {
				impalaSQL.append(" UNION ");
			}
			impalaSQL.append(timesReportSQL);
		}

		log.info("reportSQL={}", impalaSQL);

		return impalaSQL.toString();

	}

}
